import cv2
import  numpy as np
if __name__ == '__main__':
    image_np = cv2.imread('image.jpg')
    h = image_np.shape[0]
    w = image_np.shape[1]
    x_min, x_max = 505, 602
    y_min, y_max = 271, 361
    try:
        if not (x_min >= 0 and y_min >= 0 and x_max < w and y_max < h and x_max >x_min and y_max > y_min):
            raise ValueError('ROI区域设定有误')


        ROI_image = image_np.copy()[y_min:y_max, x_min:x_max]
        line_width = 2
        cv2.rectangle(image_np, (x_min, y_min), (x_max , y_max), (0, 0, 255), line_width)

    except Exception as e:
        print('程序运行出错', e)

    cv2.imshow('', ROI_image)
    cv2.waitKey(0)


